//package com.molyfun.core.shiro;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import org.apache.commons.lang3.StringUtils;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.subject.Subject;
//import com.molyfun.core.util.WebUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import com.github.pagehelper.PageInfo;
//import com.molyfun.model.generator.SysSession;
//import com.molyfun.model.generator.SysUser;
//import com.molyfun.service.sys.SysAuthorizeService;
//import com.molyfun.service.sys.SysSessionService;
//import com.molyfun.service.sys.SysUserService;
//
///**
// * 权限检查类
// * 
// */
//public class Realm extends AuthorizingRealm {
//	private final Logger logger = LogManager.getLogger();
//	@Autowired
//	private SysUserService sysUserService;
//	@Autowired
//	private SysSessionService sysSessionService;
//	@Autowired
//	private SysAuthorizeService sysAuthorizeService;
//
//	// 权限
//	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//		Integer userId = WebUtil.getCurrentUser();
//		SysUser sysUser = sysUserService.queryById(userId);
//		if (sysUser.getUserType() != 1) {
//			userId = null;
//		}
//		List<String> list = sysAuthorizeService.queryPermissionByUserId(userId);
//		for (String permission : list) {
//			if (StringUtils.isNotBlank(permission)) {
//				// 添加基于Permission的权限信息
//				info.addStringPermission(permission);
//			}
//		}
//		// 添加用户权限
//		info.addStringPermission("user");
//		return info;
//	}
//
//	// 登录验证
//	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
//			throws AuthenticationException {
//		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("countSql", 0);
//		params.put("enable", 1);
//		params.put("account", token.getUsername());
//		PageInfo<SysUser> pageInfo = sysUserService.query(params);
//		if (pageInfo.getSize() == 1) {
//			SysUser user = pageInfo.getList().get(0);
//			StringBuilder sb = new StringBuilder(100);
//			for (int i = 0; i < token.getPassword().length; i++) {
//				sb.append(token.getPassword()[i]);
//			}
//			if (user.getPassword().equals(sb.toString())) {
//				WebUtil.saveCurrentUser(user.getId());
//				saveSession(user.getAccount());
//				AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user.getAccount(), user.getPassword(),
//						user.getUserName());
//				return authcInfo;
//			}
//			logger.warn("USER [{}] PASSWORD IS WRONG: {}", token.getUsername(), sb.toString());
//			return null;
//		} else {
//			logger.warn("No user: {}", token.getUsername());
//			return null;
//		}
//	}
//
//	/** 保存session */
//	private void saveSession(String account) {
//		// 踢出用户
//		sysSessionService.deleteByAccount(account);
//		SysSession record = new SysSession();
//		record.setAccount(account);
//		Subject currentUser = SecurityUtils.getSubject();
//		Session session = currentUser.getSession();
//		record.setSessionId(session.getId().toString());
//		String host = (String) session.getAttribute("HOST");
//		record.setIp(StringUtils.isBlank(host) ? session.getHost() : host);
//		record.setStartTime(session.getStartTimestamp());
//		sysSessionService.update(record);
//	}
//}
